package com.localjoycommunity.cpdj.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.localjoycommunity.cpdj.common.utils.DateUtils;
import com.localjoycommunity.cpdj.project.system.domain.ActivityWish;
import com.localjoycommunity.cpdj.project.system.domain.UserActivity;
import com.localjoycommunity.cpdj.project.system.mapper.ActivityWishMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserActivityMapper;
import com.localjoycommunity.cpdj.project.system.service.IActivityWishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 心愿 活动Service业务层处理
 *
 * @author aqie
 * @date 2020-11-29
 */
@Service
public class ActivityWishServiceImpl implements IActivityWishService {
  @Autowired private ActivityWishMapper activityWishMapper;
  @Autowired private UserActivityMapper userActivityMapper;

  /**
   * 查询心愿 活动
   *
   * @param id 心愿 活动ID
   * @return 心愿 活动
   */
  @Override
  public ActivityWish selectActivityWishById(Long id) {
    return activityWishMapper.selectActivityWishById(id);
  }

  /**
   * 查询心愿 活动列表
   *
   * @param activityWish 心愿 活动
   * @return 心愿 活动
   */
  @Override
  public List<ActivityWish> selectActivityWishList(ActivityWish activityWish) {
    return activityWishMapper.selectActivityWishList(activityWish);
  }

  /**
   * 新增心愿 活动
   *
   * @param activityWish 心愿 活动
   * @return 结果
   */
  @Override
  public int insertActivityWish(ActivityWish activityWish) {
    activityWish.setCreateTime(DateUtils.getNowDate());
    activityWish.setUpdateTime(DateUtils.getNowDate());
    return activityWishMapper.insertActivityWish(activityWish);
  }

  /**
   * 修改心愿 活动
   *
   * @param activityWish 心愿 活动
   * @return 结果
   */
  @Override
  public int updateActivityWish(ActivityWish activityWish) {
    ActivityWish activityWishDb = activityWishMapper.selectActivityWishById(activityWish.getId());
    if (activityWishDb != null) {
      Integer type = activityWishDb.getType();
      if (type == 2) {
        // 只有活动才修改
        String shelfStatus = activityWishDb.getShelfStatus();
        // 只有下架状态可修改
        if ("0".equals(shelfStatus)) {
          LambdaQueryWrapper<UserActivity> wrapper = new LambdaQueryWrapper<>();
          wrapper
              .eq(UserActivity::getStateCode, 1)
              .eq(UserActivity::getActivityId, activityWishDb.getId());
          List<UserActivity> userActivityList = userActivityMapper.selectList(wrapper);
          if (userActivityList != null) {
            // 已报名的人数
            Integer size = userActivityList == null ? 0 : userActivityList.size();
            // 前端传过来的修改后的名额
            if(activityWish != null && activityWish.getTotalPeople()!=null){
              Long totalPeople = activityWish.getTotalPeople();
              // 只有名额比已报名的人数多才可以修改剩余名额
              if (totalPeople > size) {
                activityWish.setRemainNum(activityWish.getTotalPeople() - size);
              }
            }
          }
        }
      }
    }
    activityWish.setUpdateTime(DateUtils.getNowDate());
    return activityWishMapper.updateActivityWish(activityWish);
  }

  /**
   * 批量删除心愿 活动
   *
   * @param ids 需要删除的心愿 活动ID
   * @return 结果
   */
  @Override
  public int deleteActivityWishByIds(Long[] ids) {
    return activityWishMapper.deleteActivityWishByIds(ids);
  }

  /**
   * 删除心愿 活动信息
   *
   * @param id 心愿 活动ID
   * @return 结果
   */
  @Override
  public int deleteActivityWishById(Long id) {
    return activityWishMapper.deleteActivityWishById(id);
  }

  @Override
  @Transactional
  public int updateActivityVisitNum(Long id) {
    return activityWishMapper.updateActivityVisitNum(id);
  }
}
